﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Globalization;

namespace University.Workshop.Model.LogicalWinner
{
    public class Hand
    {
        public bool HandState { get; set;}
        //public Collection<Card> Cards { get; private set; }
        public Card[] CardsOfHand = null;
        public Hand()
        {
            HandState = false;
            CardsOfHand = new Card[7];
            for (int i = 0; i < 7; i++)
            {
                CardsOfHand[i] = new Card();
            }
        }
        public string HandForce()
        {
            
            if (RoyalFlush() != "false")
            {
                return RoyalFlush();
            }
            else
            {
                if (StraightFlush() != "false")
                {
                    return StraightFlush();
                }
                else
                {
                    if (Poker() != "false")
                    {
                        return Poker();
                    }
                    else
                    {
                        if (fullHouse() != "false")
                        {
                            return fullHouse();
                        }
                        else
                        {
                            if (flush() != "false")
                            {
                                return flush();
                            }
                            else
                            {
                                if (straight() != "false")
                                {
                                    return straight();
                                }
                                else
                                {
                                    if (threeOfDeKing() != "false")
                                    {
                                        return threeOfDeKing();
                                    }
                                    else
                                    {
                                        if (twoPair() != "false")
                                        {
                                            return twoPair();
                                        }
                                        else
                                        {
                                            if (OnePair() != "false")
                                            {
                                                return OnePair();
                                            }
                                            else
                                            {
                                                return High();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }   
            }

        }
        //11111111111111
        public string High()
        { 
            order();
            return Convert.ToString(CardsOfHand[6].NumberOfCard, CultureInfo.InvariantCulture); 
        }
        public string OnePair()
        {
            order();
            string result = "false";
            for (int i = 0; i < 6; i++)
            {
                if (CardsOfHand[i].NumberOfCard == CardsOfHand[i + 1].NumberOfCard)
                {
                    result = "15 " + CardsOfHand[i].NumberOfCard;
                    return result;
                }
            }
            return result;
        }
        //22222222222222
        private string twoPair()
        {
            order();
            int counter = 0;
            int position1 = 0;
            int position2 = 0;
            for (int i = 0; i < 6; i++)
            {
                if (CardsOfHand[i].NumberOfCard == CardsOfHand[i+1].NumberOfCard)
                {
                    counter = counter + 1;
                    if(counter == 1)
                    {
                        position1 = i;
                    }
                    if(counter == 2)
                    {
                        position2 = i;
                    }
                }
            }
            if (counter >= 2)
            {
                if (CardsOfHand[position1].NumberOfCard > CardsOfHand[position2].NumberOfCard)
                {
                    string result = "16 " + CardsOfHand[position1].NumberOfCard;
                    return result;
                }
                else
                {
                    string result = "16 " + CardsOfHand[position2].NumberOfCard;
                    return result;
                }
            }
            return "false";
        }
        //33333333333333333333333
        private string threeOfDeKing()
        {
            order();
            int counter = 0;
            for (int i = 0; i < 6; i++)
            {
                if (CardsOfHand[i].NumberOfCard == CardsOfHand[i + 1].NumberOfCard)
                {
                    counter = counter + 1;
                    if (counter == 2)
                    {
                        string result = "17 " + CardsOfHand[i].NumberOfCard;
                        return result;
                    }
                }
                else
                {
                    counter = 0;
                }
            }
            return "false";
        }
        
        //44444444444444444444444444
        private string straight()
        {
            order();
            int counter = 0;
            for (int i = 0; i < 6; i++)
            {
                if (((CardsOfHand[i].NumberOfCard) == ((CardsOfHand[i + 1].NumberOfCard) - 1)) || 
                    ((CardsOfHand[i].NumberOfCard) == (CardsOfHand[i + 1].NumberOfCard)))
                {
                    if ((CardsOfHand[i].NumberOfCard) == (CardsOfHand[i + 1].NumberOfCard) )
                    {
                        counter = counter - 1;
                    }
                    counter = counter + 1;
                    if (counter == 4)
                    {
                        string result = "18 " + CardsOfHand[i+1].NumberOfCard;
                        return result;
                    }
                }
                else
                {
                    counter = 0;
                }
            }
            return "false";
        }
        private void order()
        {
            int i, j;
            int counterOne=0;
            string counterTwo = "";
            
            for (i = 0; i <= 5; i++)
            {
                for (j = 0; j <= 5-i; j++)
                {
                    if (CardsOfHand[j].NumberOfCard > CardsOfHand[j+1].NumberOfCard )
                    {
                        counterOne = CardsOfHand[j].NumberOfCard;
                        counterTwo = CardsOfHand[j].TypeOfCard;

                        CardsOfHand[j].NumberOfCard = CardsOfHand[j + 1].NumberOfCard ;
                        CardsOfHand[j].TypeOfCard = CardsOfHand[j + 1].TypeOfCard;

                        CardsOfHand[j + 1].NumberOfCard =  counterOne ;
                        CardsOfHand[j + 1].TypeOfCard = counterTwo;
                    }
                }
            }
        }
        //5555555555555555
        private string flush()
        {
            int clover = 0;
            int sword = 0;
            int heart = 0;
            int diamont = 0;

            for (int i = 0; i < 7; i++)
            {
                if (CardsOfHand[i].TypeOfCard == "clover")
                {
                    clover = clover + 1;
                }
                if (CardsOfHand[i].TypeOfCard == "sword")
                {
                    sword = sword + 1;
                }
                if (CardsOfHand[i].TypeOfCard == "heart")
                {
                    heart = heart + 1;
                }
                if (CardsOfHand[i].TypeOfCard == "diamont")
                {
                    diamont = diamont + 1;
                }
            }
            if ( clover > 4 )
            {
                string result=highestNumberOfColor("clover");
                return ("19 "+result);
            }
            if ( sword > 4 )
            {
                string result = highestNumberOfColor("sword");
                return ("19 "+result);
            }
            if ( heart > 4 )
            {
                string result = highestNumberOfColor("heart");
                return ("19 "+result);
            }
            if ( diamont > 4 )
            {
                string result = highestNumberOfColor("diamont");
                return ("19 "+result);
            }
            return "false";
        }
        private string highestNumberOfColor(string color)
        {
            string result = "1";
            for (int i = 0; i < 7; i++)
            {
                if (CardsOfHand[i].TypeOfCard == color)
                {
                    if (Convert.ToInt32(result,CultureInfo.InvariantCulture) < CardsOfHand[i].NumberOfCard)
                    {
                        result = Convert.ToString(CardsOfHand[i].NumberOfCard , CultureInfo.InvariantCulture);
                    }
                }
            }
            return result;
        }
        //666666666666
        private string fullHouse()
        {
            order();
            char[] list = {' '};
            string[] response = (threeOfDeKing() ).Split(list);
            if ( response[0] == "17")
            {
                if (onlyOnePair())
                {
                    string result = "20 " + response[1];
                    return result;    
                }                
            }
            return "false";
        }
        private bool onlyOnePair()
        {
            for (int i = 0; i < 5; i++)
            {
                if (CardsOfHand[i].NumberOfCard == CardsOfHand[i + 1].NumberOfCard && CardsOfHand[i + 1].NumberOfCard == CardsOfHand[i + 2].NumberOfCard)
                {
                    i = i + 2;
                }
                else
                {
                    if (CardsOfHand[i].NumberOfCard == CardsOfHand[i + 1].NumberOfCard)
                    {
                        return true;
                    }
                }
            }
            if ( (CardsOfHand[5].NumberOfCard == CardsOfHand[6].NumberOfCard) && CardsOfHand[4].NumberOfCard != CardsOfHand[5].NumberOfCard )
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //77777777777777777777777
        public string Poker()
        {
            order();
            int counter=0;
            for (int i = 0; i < 6; i++)
            {
                if (CardsOfHand[i].NumberOfCard == CardsOfHand[i + 1].NumberOfCard)
                {
                    counter = counter + 1;
                    if (counter == 3)
                    {
                        string result = "21 " + CardsOfHand[i].NumberOfCard;
                        return result;
                    }
                }
                else
                {
                    counter = 0;
                }
            }
            return "false";
        }
        //888888888888888888888
        public string StraightFlush()
        {
            order();
            int counter = 0;
            for (int i = 0; i < 6; i++)
            {
                if (  (   (CardsOfHand[i].TypeOfCard == CardsOfHand[i + 1].TypeOfCard) &&(CardsOfHand[i].NumberOfCard == ((CardsOfHand[i + 1].NumberOfCard)-1) )   ) /*||
                    (cards[i].getNumber() == (cards[i + 1].getNumber())) ||
                    ((cards[i-1].getType() == cards[i + 1].getType()) && (cards[i-1].getNumber() == ((cards[i + 1].getNumber()) - 1)))  ||
                    (cards[i].getNumber() == ((cards[i + 1].getNumber())-1)) */  )
                {
                    if ((CardsOfHand[i].NumberOfCard == (CardsOfHand[i + 1].NumberOfCard)) /*|| (cards[i].getNumber() == ((cards[i + 1].getNumber()) - 1)) */)
                    {
                        counter = counter - 1;
                    }
                    counter = counter + 1;
                    if (counter == 4)
                    {
                        string auxiliary = highestNumberOfColor(CardsOfHand[i].TypeOfCard);
                        return ("22 "+auxiliary);
                    }
                }
                else
                {
                    counter = 0;
                }
            }
            return "false";
        }
        //99999999999999999
        public string RoyalFlush()
        {
            order();
            int counter = 0;
            for (int i = 0; i < 6; i++)
            {
                if ((CardsOfHand[i].TypeOfCard == CardsOfHand[i + 1].TypeOfCard) && (CardsOfHand[i].NumberOfCard == ((CardsOfHand[i + 1].NumberOfCard) - 1)))
                {
                    counter = counter + 1;
                    if (counter == 4 && highestNumberOfColor(CardsOfHand[i].TypeOfCard) == "14" )
                        return "23";
                }
                else
                {
                    counter = 0;
                }
            }
            return "false";
        }
    }
}
